using System;
using System.Drawing;
using System.Collections.Generic;
using TTDotNet.Engine.Stations.Depot;
using TTDotNet.Engine.GUI.Windows;
using TTDotNet.Engine.GUI.Buttons;
using TTDotNet.Engine.Vehicles;
using TTDotNet.Engine.GUI.Windows.Depot.Vehicles;
using Microsoft.DirectX.Direct3D;

namespace TTDotNet.Engine.GUI.Windows.Depot
{
    public class RoadDepotWindow : Window
    {
        private int cSize = 24;
        private int border = 2;
        private int vehiclesPerRow = 5;
        private int vehicleRows = 3;
        private int vehicleWidth = 75;
        private int vehicleHeight = 25;
        private int binWidth = 60;
        private RoadDepot depot;
        private int listboxNum;

        private NewVehiclesWindow vehcWindow;

        public RoadDepotWindow(PointF screenLocation, RoadDepot depot)
        {
            this.depot = depot;
            int width = (vehiclesPerRow * vehicleWidth) + ((vehiclesPerRow + 3) * border) + binWidth;
            int height = (2 * cSize) + (vehicleRows * vehicleHeight) + ((vehicleRows * 2 + 4) * border);

            SetScreenRectangle(new RectangleF(screenLocation, new SizeF(width, height)));

            int controlNum = 0;

            windowControls[controlNum++] = new Label(new RectangleF(new PointF(0, 0), new SizeF(width, height)), "", 1, Color.Black, "Arial",
                                            Game.textureCacher.LoadTexture("artwork/colours/dark_spring_green.jpg"), DrawTextFormat.Center | DrawTextFormat.VerticalCenter);

            //Close button. 
            windowControls[controlNum++] = new CloseButton(new RectangleF(new PointF(border, border), new SizeF(cSize, cSize)),
                                            Game.textureCacher.LoadTexture("artwork/128/close.jpg"), Game.textureCacher.LoadTexture("artwork/128/closeDown.jpg"), this);

            windowControls[controlNum++] = new TitleBar(new RectangleF(new PointF(cSize + (2 * border), border), new SizeF(width - (cSize + (3 * border)), cSize)),
                                            depot.Name, 14, Color.White, "Arial", Game.textureCacher.LoadTexture("artwork/colours/ttdOrange.jpg"), this);

            float lbWidth = (vehiclesPerRow * vehicleWidth) + (vehiclesPerRow + 1 * border) + cSize;

            windowControls[controlNum++] = new ListBox(new RectangleF(new PointF(border, cSize + (2 * border)), new SizeF(lbWidth, vehicleRows * (2 * border + vehicleHeight))),
                                            18, "", 1, Color.Black, "Arial", vehicleHeight + (border * 2), Game.textureCacher.LoadTexture("artwork/colours/black.jpg"));
            listboxNum = controlNum - 1;

            ListBox lb = (windowControls[listboxNum] as ListBox);

            SetupItems(lb);

            float bWidth = (width - cSize - (5 * border)) / 3;

            windowControls[controlNum++] = new GameButton(new RectangleF(new PointF(border, height - cSize - border), new SizeF(bWidth, cSize)), "New Vehicles", 14, Color.Black, "Arial",
                                            Game.textureCacher.LoadTexture("artwork/colours/ttdGrey.jpg"), Game.textureCacher.LoadTexture("artwork/colours/silver.jpg"));
            windowControls[controlNum -1].OnControl += new Control.ControlDelegate(OpenRoadVehiclesWindow);

            windowControls[controlNum++] = new GameButton(new RectangleF(new PointF((2 * border) + bWidth, height - cSize - border), new SizeF(bWidth, cSize)), "Clone Vehicle", 14, Color.Black, "Arial",
                                            Game.textureCacher.LoadTexture("artwork/colours/ttdGrey.jpg"), Game.textureCacher.LoadTexture("artwork/colours/silver.jpg"));

            windowControls[controlNum++] = new GameButton(new RectangleF(new PointF((3 * border) + (2 * bWidth), height - cSize - border), new SizeF(bWidth, cSize)), "Location", 14, Color.Black, "Arial",
                                            Game.textureCacher.LoadTexture("artwork/colours/ttdGrey.jpg"), Game.textureCacher.LoadTexture("artwork/colours/silver.jpg"));

        }

        private void SetupItems(ListBox lb)
        {
            lb.MultiLine = true;
            int slots = vehicleRows * vehiclesPerRow;

            for (int row = 0; row < vehicleRows; row++)
            {
                ListableItem listItem = lb.AddItem();

                for (int vehc = 0; vehc < vehiclesPerRow; vehc++)
                {
                    Label l = new Label(new RectangleF(((vehc + 1) * border) + (vehc * vehicleWidth), border, vehicleWidth, vehicleHeight),
                                            "", 14, Color.White, "Arial", Game.textureCacher.LoadTexture("artwork/colours/ttdGrey.jpg"), DrawTextFormat.Center);
                    listItem.AddControl(l);
                }
            }
        }

        private List<Control> GetBusItem(RoadVehicle vehicle)
        {
            return null;
        }

        private void OpenRoadVehiclesWindow(object data, object tag)
        {
            if (Game.newGUI.IsOpen(vehcWindow) == false || vehcWindow == null)
            {
                vehcWindow = new NewVehiclesWindow(new PointF(screenRectangle.X + 200, screenRectangle.Y + 200), VehicleType.Bus, depot);
                Game.newGUI.OpenNewWindow(vehcWindow);
            }
            else
            {
                if (Game.newGUI.IsOpen(vehcWindow)) vehcWindow.Close();
            }
        }

    }
}
